﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace MyMusicPlay._4._0
{
    public class MyMessageBox : ViewBoard
    {
        private MainWindow _main;
        private bool backMessaageJudge = false;
        public Grid backGrid;//控制背板
        public StackPanel messageBox;//控制弹窗与内容
        public Border messageBorder;//控制边框与弹窗大小
        private Action butYesAction;
        private Size size = new Size(350, 200);//默认大小

        private static MyMessageBox MyMessage;

        public MyMessageBox(MainWindow main) : base(main)
        {
            MyMessage = this;
            _main = main;
        }

        public override void Initialize()
        {
            backGrid = _main.BackMyMessageBox;
            messageBox = _main.MyMessageStap;
            messageBorder = _main.MyMessageBorder;
        }

        /// <summary>
        /// 拉起背板
        /// </summary>
        /// <param name="action">点击后的回调</param>
        public static void Show(string text, Action action = null)
        {
            if (MyMessage.backMessaageJudge)
                return;
            PlayApplication.GetModule<MyMessageBox>().Show();
            MyMessage.butYesAction = action;
            PlayApplication.GetModule<Rander>().CreateDefaultBoxTemplate(text);
        }
        public static void Show(FrameworkElement frameworkElement)
        {
            if (MyMessage.backMessaageJudge)
                return;
            PlayApplication.GetModule<MyMessageBox>().Show();

            MyMessage.messageBox.Children.Add(frameworkElement);
        }

        /// <summary>
        /// 显示必备的初始化
        /// </summary>
        public override void Show(ViewBoard child = null)
        {
            base.Show(this);
            backMessaageJudge = true;
            backGrid.Visibility = System.Windows.Visibility.Visible;
            //创建动画
            messageBorder.Height = 0;
            SY_Library.Animate.Deformation(messageBorder, SY_Library.Animate.direction.Height, messageBorder.Height, size.Height, 100);
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        public override void Hidden()
        {
            if (!MyMessage.backMessaageJudge)
                return;
            //创建动画
            SY_Library.Animate.Deformation(MyMessage.messageBorder, SY_Library.Animate.direction.Height, MyMessage.messageBorder.Height, 0, 100);
            base.Hidden();
            Task.Run(() =>
            {
                Task.Delay(120).Wait();
                MyMessage.messageBorder.Dispatcher.Invoke(() =>
                {
                    MyMessage.backMessaageJudge = false;
                    MyMessage.backGrid.Visibility = System.Windows.Visibility.Hidden;
                    MyMessage.messageBox.Children.Clear();
                });
            });
        }

        /// <summary>
        /// 确定按钮的点击
        /// </summary>
        public void YesButton_Click(object sender, RoutedEventArgs e)
        {
            if (butYesAction == null)
            {

            }
            else
            {
                butYesAction();
            }
            Hidden();
        }
    }
}
